package engine

import (
	"01-crawler-project/01/fetcher"
	"fmt"
	"log"
)

func Run(seeds ...Request) {

	if len(seeds) <= 0  {

		return
	}

	var requests [] Request

	for _, v := range seeds {

		requests = append(requests, v)
	}

	for len(requests) > 0 {

		req := requests[0]

		fmt.Printf("httpRequest is : %s \n", req.HttpRequest.URL)

		requests = requests[1:]

		bytes, e := fetcher.Fetcher(req.HttpRequest)

		if e != nil {

			log.Printf("fetcher err : %s \n", e)

		}

		results := req.ParserFunc(bytes)

		for k, _ := range results.Items {

			fmt.Printf("item is : %s \n", results.Items[k])

		}

		if len(results.Requests) > 0 {
			Run(results.Requests...)
		}

		//fmt.Printf("size : %d", len(results.Items))

	}

}
